package pageTest.login;

import constants.HostConstant;
import constants.UserInfoConstant;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import utils.WebDriverUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;

@Slf4j
public class MessagePage extends WebDriverUtils {
    private static final String MESSAGE_PAGE_URL = "http://" + HostConstant.HOST + ":5173/message";

    public MessagePage() {
        super(MESSAGE_PAGE_URL);
    }

    @BeforeClass
    public void setUp() {
        driver.get(super.LOGIN_PAGE_URL);
        driver.findElement(By.cssSelector("#van-field-1-input")).sendKeys(UserInfoConstant.USER_ACCOUNT);
        driver.findElement(By.cssSelector("#van-field-2-input")).sendKeys(UserInfoConstant.USER_PASSWORD);
        driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round")).click();
        // 确保登录成功了(登录操作响应已经返回， cookie 被种上)
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "CAMPUS 伙伴匹配"));
    }

    @BeforeMethod
    public void beforeEachTest() {
        driver.get(MESSAGE_PAGE_URL);
    }


    @Test
    public void pageRight() {
        // 部导航栏标题为“消息”
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "消息"));
        List<WebElement> elements = driver.findElements(By.tagName("span"));
        // 2. “关注”和“粉丝” 文字存在
        assert "关注".equals(elements.get(0).getText());
        assert "粉丝".equals(elements.get(1).getText());
        // 3.
    }

    @Test
    public void buttonClickable() {
        String topNavBarSlc = "#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis";
        // 1. 点击 “关注” 按钮页面正确跳转
        driver.findElement(By.cssSelector("#messagePage > div.van-grid > div:nth-child(1)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "关注"));
        // 2. 点击 “粉丝” 按钮页面正确跳转
        driver.navigate().back();
        driver.findElement(By.cssSelector("#messagePage > div.van-grid > div:nth-child(2)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "粉丝"));
        // 3. 点击 AI助手 新增一个网页
        driver.navigate().back();
        driver.findElement(By.cssSelector("#messagePage > div:nth-child(3)")).click();
        Set<String> windowHandles = driver.getWindowHandles();
        String curWindowHandle = driver.getWindowHandle();
        for (String windowHandle : windowHandles) {
            // 切换到新打开的窗口
            if (!windowHandle.equals(curWindowHandle)) {
                driver.switchTo().window(windowHandle);
                break;
            }
        }
        assert driver.getTitle().equals("AIchatOS2");
//        String text = driver.findElement(By.cssSelector("#app > div > div.h-full.dark\\:bg-\\[\\#24272e\\].transition-all.p-4 > div > div > div > aside > div.n-layout-sider-scroll-container > div > main > div:nth-child(1) > button > span")).getText();
//        assert text.equals("New chat");
        driver.switchTo().window(curWindowHandle);
    }

    @Test
    public void unreadCountCueRight() {
        // 1. 消息角标正确
        int friendApplyCount = 0;
        int unreadMessageCount = 0;
        try {
            String count = driver.findElement(By.cssSelector("#messagePage > div:nth-child(4) > div.van-badge__wrapper > div.van-badge.van-badge--top-right.van-badge--fixed")).getText();
            friendApplyCount = Integer.parseInt(count);
        } catch (Exception e) {
            log.info("当前没有好友申请");
        }
        try {
            String count = driver.findElement(By.cssSelector("#messagePage > div:nth-child(5) > div.van-badge__wrapper > div.van-badge.van-badge--top-right.van-badge--fixed")).getText();
            unreadMessageCount = Integer.parseInt(count);
        } catch (Exception e) {
            log.info("当前没有私聊消息");
        }
        int totalCount = 0;
        try {
            String count = driver.findElement(By.cssSelector("#app > div.van-tabbar.van-tabbar--fixed.van-hairline--top-bottom.van-safe-area-bottom > div.van-tabbar-item.van-tabbar-item--active > div.van-badge__wrapper.van-tabbar-item__icon > div")).getText();
            totalCount = Integer.parseInt(count);
        } catch (Exception e) {
            log.info("当前没有未读提示");
        }
        assert totalCount == friendApplyCount + unreadMessageCount;

        // 2. 若当前有好友申请，则点击后有若干个 “同意” 按钮
        if (friendApplyCount > 0) {
            driver.findElement(By.cssSelector("#messagePage > div:nth-child(4)")).click();
            wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("van-button__text")));
            List<WebElement> elements = driver.findElements(By.className("van-button__text"));
            int agreeButtonCount = 0;
            for (WebElement element : elements) {
                if (Objects.equals("同意", element.getText())) {
                    agreeButtonCount++;
                }
            }
            assert friendApplyCount == agreeButtonCount;
            driver.navigate().back();
        }

        // 3. 若当前有未读私聊消息，则红色角标数相加与 unreadMessageCount 一致
        if (unreadMessageCount > 0) {
            driver.findElement(By.cssSelector("#messagePage > div:nth-child(5)")).click();
            int tmpCount = 0;
            wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("#app > div.content > div:nth-child(2) > div > div.van-badge__wrapper > div.van-badge.van-badge--top-right.van-badge--fixed")));
            List<WebElement> numbers = driver.findElements(By.cssSelector("#app > div.content > div:nth-child(2) > div > div.van-badge__wrapper > div.van-badge.van-badge--top-right.van-badge--fixed"));
            for (WebElement number : numbers) {
                tmpCount += Integer.parseInt(number.getText());
            }
            assert tmpCount == unreadMessageCount;
            driver.navigate().back();
        }
//        #userFriendsPage > div:nth-child(1) > div > div > div.van-card__bottom > div > button.van-button.van-button--primary.van-button--mini.van-button--plain
//            #userFriendsPage > div:nth-child(2) > div > div > div.van-card__bottom > div > button
//                #userFriendsPage > div:nth-child(1) > div > div > div.van-card__bottom > div > button
    }

    @Test
    public void canChat() {
        // 1. “消息”页面中，其中某个聊天室显示的最后一条信息与点进去显示的最后一条信息一致
        driver.findElement(By.cssSelector("#messagePage > div:nth-child(6)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "大厅聊天室"));
        // 2. 发送一条消息后刷新页面，最后一条消息与发送消息内容一致
        String sendMessage = "auto send message test";
        driver.findElement(By.tagName("input")).sendKeys(sendMessage);
        driver.findElement(By.cssSelector("#app > div.chat-container > div.van-cell-group.van-cell-group--inset > div > div > div > div > button")).click();
        // 刷新页面
        driver.navigate().refresh();
        List<WebElement> elements = driver.findElements(By.className("text"));
        String lastText = elements.get(elements.size() - 1).getText();
        assert sendMessage.equals(lastText);
    }


    @AfterClass
    public void tearDown() {
        driver.quit();
    }

}
